import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-30
 * Time: 14:48
 */
//模拟实现一个线程池
class MyThreadPool{
    //将添加到线程池中的任务可以放置到这个队列中
    private BlockingQueue<Runnable> queue =new LinkedBlockingQueue<>();
    //通过这个方法可以将任务添加到线程池中
    public void  submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //构造函数，创建一个线程池,数量为n
    public  MyThreadPool(int n){
        for (int i = 0; i <n; i++) {
            Thread t=new Thread(() ->{
               while(true){
                   try {
                       //取放入线程池队列中的任务进行执行
                       Runnable runnable= queue.take();
                       runnable.run();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
            });
            t.start();
        }
    }

}
public class Demo23 {
    public static void main(String[] args) throws InterruptedException {
    MyThreadPool myThreadPool=new MyThreadPool(4);
        for (int i = 0; i <10; i++) {
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(
                            Thread.currentThread().getName()+"hello"
                    );
                }
            });
        }
    }
}
